<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\QuestionType;
use think\Db;
class Dorms extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 扣分列表
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if(isset($param['id'])){
            if (isset($param['leader'])) {
                $where['leader'] = $param['leader'];
            }
            $where['group'] = $param['id'];
            $page = input('page')?:0;//当前页数
            $list=model('dorms')->where('status',['>',-1])->where($where)->paginate(config('page_number'));

            return $this->fetch('index', ['list' =>$list,'page'=>$page,'id'=>$param['id']]);
        }
        $this->error('请选择宿舍');
    }
    public function edit()
    {
        $param = $this->request->param();
        $user=$param['user'];
        unset($param['user']);
        if ($this->request->isPost()) {
            if ($this->update('dorms', $param, input('_verify', true)) === true) {
                insert_admin_log('修改了原因');
                $this->success('修改原因成功', url('admin/dorms/index',['id' => $user]));
            } else {
                $this->error($this->errorMsg,['id' => $user]);
            }
        }
        $project=model('project')->select();
        return $this->fetch('adddorms', ['data' => model('dorms')->where('id', $param['id'])->find(),'user'=>$user,'project'=>$project]);
    }
    /**
     * 删除原因
     */
    public function del()
    {
        if ($this->request->isGet()) {
            $param = $this->request->param();
            $user=$param['user'];
            unset($param['user']);
            if ($this->delete('dorms', $param ) === true) {
                insert_admin_log('删除了用户');
                $this->success('删除成功', url('admin/dorms/index',['id' => $user]));
            } else {
                $this->error($this->errorMsg,['id' => $user]);
            }
        }
    }


    /**
     * 原因状态设置
     */
    public function teacherstatus(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if($this->update('dorms', ['id'=>$param['id'],'status'=>$param['status']], input('_verify', true)) and $this->update('dorms', ['id'=>$param['id'],'status'=>$param['status']], input('_verify', true),$field = true )){
               $this->success('操作成功！', url('admin/dorms/index'));
            } else{
               $this->error($this->errorMsg);
            }
            $this->success('操作成功', url('admin/dorms/index'));
        }
    }
    /**
     * 设置组长
     */
    public function leader(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $data=model('dorms')->where(['id'=>$param['user'],'leader'=>1])->find();
            if(!empty($data)){
                if(!$this->update('dorms', ['id'=>$data['id'],'leader'=>0])){
                    $this->error('设置组长失败');
                };
            }
            if($this->update('dorms', ['id'=>$param['id'],'leader'=>$param['leader']], input('_verify', true)) and $this->update('dorms', ['id'=>$param['id'],'leader'=>$param['leader']], input('_verify', true),$field = true )){
                $this->success('操作成功！', url('admin/dorms/index'));
            } else{
                $this->error($this->errorMsg);
            }
            $this->success('操作成功', url('admin/dorms/index'));
        }
    }


}
